/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-05-30 19:22:40
 * @LastEditTime: 2022-05-31 15:03:14
 */

import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';
import {
  ADD_ALIAS,
  DELETE_ALIAS,
  EDIT_ALIAS,
  SET_ALIAS_LIST,
  SET_SELECTED_ITEM
} from '../constants';

export const MetadataAliasReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    // 设置移交清单列表
    case SET_ALIAS_LIST:
      return {
        ...state,
        aliasList: action.data
      };

    // 增加移交清单
    case ADD_ALIAS:
      return {
        ...state,
        aliasList: insertItem(action.data, state.aliasList)
      };

    // 增加移交清单
    case EDIT_ALIAS:
      return {
        ...state,
        aliasList: editItemById(action.id, action.data, state.aliasList)
      };

    // 删除移交清单
    case DELETE_ALIAS:
      return {
        ...state,
        aliasList: delItemByIdArray(action.ids, state.aliasList)
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
